<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input v-model="form.account" class="input" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input v-model.trim="form.mobile" class="input" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input v-model="form.code" class="input" type="text" placeholder="请输入验证码" />
        <span @click="sendCode()" class="code">{{ countTime === 0 ? '发送验证码' : `${countTime}秒后尝试` }}</span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input v-model="form.password" class="input" type="password" placeholder="请输入密码" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input v-model="form.rePassword" class="input" type="password" placeholder="请确认密码" />
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" @click="submit()" class="submit">立即提交</a>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue-demi'
import { message } from '@/components/Message'
import { reqUserQQPatchCode, reqUserQQPatchLogin } from '@/api/user'
import { useCountDown } from '@/hooks'
import { useStore } from '@/store'
import { useRouter } from 'vue-router'

const { user, cart } = useStore()
const router = useRouter()

const props = defineProps({
  unionId: {
    type: String,
    default: ''
  }
})
// 表单对象
const form = reactive({
  account: '',
  mobile: '', // 使用自己的手机号码，一个QQ号只能绑一个手机
  code: '123456',
  password: '',
  rePassword: ''
})

// 调用倒计时函数 countTime 倒计时数字 , start 启动倒计时的函数
const { countTime, start } = useCountDown(60)

const sendCode = async () => {
  // 先校验手机号码，有错误退出函数并提示用户
  if (!/^1[3-9]\d{9}$/.test(form.mobile)) return message({ text: '手机号码格式错误' })
  // 发送手机验证码
  await reqUserQQPatchCode(form.mobile)
  // 发送验证码成功就启动倒计时
  start()
}

// 注册 - 💥💥💥注意：机会只有一次
const submit = async () => {
  // 密码校验
  if (form.password !== form.rePassword) return message({ text: '两次密码不一致' })
  // QQ授权 + 手机号 注册
  const { result } = await reqUserQQPatchLogin({
    // 父组件接收
    unionId: props.unionId,
    // 表单信息
    ...form
  })
  // 1. result 存储着用户的信息，可以直接存储到 Vuex 仓库中
  await user.setProfile(result)
  // 🔔 合并购物车
  await cart.mergeLocalCart()
  // 2. 跳转页面
  router.push('/')
  // 3. 提示用户
  message({ type: 'success', text: 'QQ完善信息成功' })
}

</script>

<style scoped lang="scss">

.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
